home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.07 Jul 91 / Drivers / MenuMem INIT.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-03  |  5.5 KB  |  202 lines  |  [TEXT/KAHL]

  1. /*------------------------------------------------------------------------------
  2.  
  3. PROGRAM
  4.         MenuMem - A combination INIT/DRVR that displays the amount of
  5.                   free memory in the menu bar.
  6.  
  7. FILE
  8.         “MenuMem INIT.c”
  9.  
  10. DESCRIPTION
  11.         This file contains all of the code for the INIT portion of the program.
  12.         It loads the DRVR and its associated DATA into the system heap. 
  13.  
  14. HISTORY
  15.         2/14/90    -    DAG    Version .1
  16.  
  17.             Copyright © 1990 Daniel A. Green.  All rights reserved.
  18.  
  19. ------------------------------------------------------------------------------ */
  20.  
  21.  
  22.  
  23. /* --------------------------------- INCLUDE FILES --------------------------- */
  24. #include    "MacTypes.h"
  25. #include    "string.h"
  26. #include    "MenuMgr.h"
  27. #include    <DeviceMgr.h>
  28. #include     <ResourceMgr.h>
  29. #include    "MenuMem.h"
  30.  
  31.  
  32.  
  33. /* ---------------------------------  PROTOTYPES  ---------------------------- */
  34. Boolean        DRVRXistsP(Integer theID);
  35. Boolean        slot_takenP(Integer slot_num);
  36. Boolean        vacant_slotP(Integer sun, Integer *the_slot);
  37.  
  38.  
  39.  
  40. /* ---------------------------------  FUNCTIONS  ----------------------------- */
  41. pascal void main()
  42. {
  43.     Integer    currentID, newID;
  44.     ResType    r_type;
  45.     Str255    r_name;
  46.     OSErr    theErr;
  47.     Handle    theDATA, theDRVR, theINIT;
  48.  
  49.     theINIT = GetNamedResource('INIT', DRVR_NAME);
  50.     GetResInfo(theINIT, ¤tID, &r_type, &r_name);
  51.  
  52. /*******************************************************************************
  53.  *
  54.  *    If there is an installed DRVR with the same ID as ours, then the resource
  55.  *    ID of our DRVR and its DATA should be changed.
  56.  *
  57.  *******************************************************************************/
  58.     if ( DRVRXistsP(currentID) )
  59.         if ( vacant_slotP(DRVR_RSRCID, &newID) ) {
  60.  
  61.             SetResInfo(theINIT, newID, &r_name);
  62.  
  63.             theDRVR = GetResource('DRVR', currentID);
  64.             GetResInfo(theDRVR, ¤tID, &r_type, &r_name);
  65.             SetResInfo(theDRVR, newID, &r_name);
  66.  
  67.             theDATA = GetResource('DATA', OWNEDRSRCID( REFNUM(currentID) ) );
  68.             GetResInfo(theDATA, ¤tID, &r_type, &r_name);
  69.             SetResInfo(theDATA, OWNEDRSRCID( REFNUM(newID) ), &r_name);
  70.  
  71.             currentID = newID;
  72.         } else {    /* Max number of DRiVeRs installed.  Just beep and return */
  73.             SysBeep(1);
  74.             return;
  75.         }
  76.  
  77. /* If we made it this far we can open the driver up */
  78.     if ( LoadDRVR(&r_name, currentID) ) SysBeep(2);
  79. };
  80.  
  81.  
  82.  
  83. /*******************************************************************************
  84.   LoadDRVR
  85.  *    Loads the DRiVeR into memory and opens it.  The System Heap attribute for
  86.  *    both the DRVR and DATA resources should be set to insure that neither will
  87.  *    be lost when the application heap is reinitialized.
  88.  *
  89.  *******************************************************************************/
  90.  
  91. LoadDRVR(theName, theID)
  92. Str255    *theName;
  93. Integer theID;
  94. {
  95.     Integer    theRefNum;
  96.     OSErr    theErr;
  97.     ResType    theType;
  98.     Handle    h, theDRVR;
  99.  
  100.     h = GetResource('DRVR', theID);
  101.     theErr = RESERROR();
  102.     if (h && (theErr == noErr) )
  103.         if ( (theErr = OpenDriver(theName, &theRefNum)) == noErr )
  104.             DetachResource(h);
  105.  
  106.     return(theErr);
  107. };
  108.  
  109.  
  110.  
  111. /*******************************************************************************
  112.   DRVRXistsP
  113.  *    returns TRUE if a DRiVeR with an ID of ‘theID’ exists in the resource fork
  114.  *    of the System File or a DRiVeR with an ID of ‘theID’ is in the Unit Table.
  115.  *    Otherwise returns FALSE.
  116.  *
  117.  *******************************************************************************/
  118.  
  119. Boolean    DRVRXistsP(theID)
  120. Integer    theID;
  121. {
  122.     Boolean    result = FALSE;
  123.     Integer    theRefNum;
  124.     Handle    h;
  125.  
  126. /* Save the reference number of our resource file */
  127.     theRefNum = CurResFile();
  128.     UseResFile(0);            /* Use the System resource file */
  129.     SetResLoad(FALSE);
  130.     h = GetResource('DRVR', theID);
  131.  
  132. /* If we can’t get a handle to the resource, then check the unit table  */
  133.     if ( h || slot_takenP(theID) ) result = TRUE;
  134.     if (h) ReleaseResource(h);
  135.     SetResLoad(TRUE);
  136.     UseResFile(theRefNum);
  137.     return(result);
  138. };
  139.  
  140.  
  141.  
  142. /*******************************************************************************
  143.   slot_takenP
  144.  *    If we can get a DCtlEntry for a given unit number, then a DRiVeR with the
  145.  *    given unit number has already been installed, so the function returns TRUE.
  146.  *    Otherwise it returns FALSE.
  147.  *
  148.  *******************************************************************************/
  149.  
  150. Boolean    slot_takenP(unit_number)
  151. Integer    unit_number;
  152. {
  153.     return( GetDCtlEntry( REFNUM( unit_number ) ) ? TRUE : FALSE );
  154. };
  155.  
  156.  
  157.  
  158. /*******************************************************************************
  159.   vacant_slotP
  160.  *    Repeatedly searches the System file and checks the unit table until an ID
  161.  *    has been found for which:
  162.  *            1).  No resource of type DRVR exists in the System file, and
  163.  *            2).  No entry exists in the unit table.
  164.  *    If an ID that satisfies the above criteria is found, the function sets
  165.  *    the_slot to the ID and returns TRUE.  Otherwise it returns FALSE.
  166.  *
  167.  *******************************************************************************/
  168.  
  169. Boolean    vacant_slotP(sun, the_slot)
  170. Integer    sun;                /* the Starting Unit Number */
  171. Integer    *the_slot;
  172. {
  173.     Boolean vacant_slot = FALSE;
  174.     Integer    i, max, refnum, theRefNum;
  175.     Handle    h;
  176.  
  177.     max = *(Integer *) (UnitNtryCnt);
  178.  
  179.     theRefNum = CurResFile();
  180.     UseResFile(0);            /* Use the System resource file        */
  181.     SetResLoad(FALSE);        /* Don’t want to load the resources    */
  182.                             /* Just check for their existence    */
  183.     i = sun;
  184.     do {
  185.         h = GetResource('DRVR', i);
  186.         if (h == NULL) {    /* Check the unit table */
  187.             refnum = (-i) - 1;
  188.             if ( GetDCtlEntry(refnum) == NULL) {
  189.                 vacant_slot = TRUE;
  190.                 *the_slot = i;
  191.             }
  192.         } else
  193.             ReleaseResource(h);
  194.         i++;
  195.     } while ( !vacant_slot && (i < max) );
  196.  
  197.     SetResLoad(TRUE);
  198.     UseResFile(theRefNum);        /* Reset the search path */
  199.  
  200.     return(vacant_slot);
  201. };
  202.